﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using IRIS.Models;
namespace IRIS.Controllers
{
    public class LinkCategryController : Controller
    {
        //
        // GET: /LinkCategry/
        DataRepo m_Db = new DataRepo();
        public ActionResult Index(int? id)
        {
            if(id.HasValue)
            {
                
                var model = from c in m_Db.getLinks()
                            where c.CID == id.Value
                            select c;
                LinkViewModel VM = new LinkViewModel {m_links = model, m_linkCategories = m_Db.getLinkCategories(),m_linkCategory = m_Db.getLinkCategoryById(id.Value)};
                return View(VM);
            }
            return View("NotFound"); 
            
        }

        public ActionResult List()
        {
            var model = m_Db.getLinkCategories();
            return View(model);
        }

        //
        // GET: /LinkCategry/Create
        [Authorize(Roles = "Stjórnendur")]
        public ActionResult Create()
        {
            return View();
        }

        //
        // POST: /LinkCategry/Create
        [Authorize(Roles = "Stjórnendur")]
        [HttpPost]
        public ActionResult Create(LinkCategory linkCategory)
        {
            //Validation for user input , Alive will be handled here also by us
            if (string.IsNullOrEmpty(linkCategory.Description))
                ModelState.AddModelError("Description", "Þessi reitur verður að vera útfylltur");
            if (string.IsNullOrEmpty(linkCategory.Name))
                ModelState.AddModelError("Name", "Þessi reitur verður að vera útfylltur");
            if(m_Db.getSizeOfAliveLinkCategories()>=10)
                ModelState.AddModelError("Name","Hámark 10 flokkar - eyða þarf flokki");
            linkCategory.Alive = true;
            try
            {
                if (ModelState.IsValid)
                {
                    //everything is alright and we commit to database
                    m_Db.addLinkCategory(linkCategory);
                    return RedirectToAction("List");
                }
                
                    //something is wrong and we return the model back with error state
                    return View(linkCategory);
                
            }
            catch
            {
                //Something is not o.k so we return error state
                return View(linkCategory);
            }
        }

        //
        // GET: /LinkCategry/Edit/5
        [Authorize(Roles = "Stjórnendur")]
        public ActionResult Edit(int? id)
        {
            if (id.HasValue)
            {
                int realId = id.Value;
                var model = m_Db.getLinkCategoryById(realId);
                return View(model);
            }
           
                return View("NotFound");
            

        }

        //
        // POST: /LinkCategry/Edit/5
        // Same logic as in create
        [Authorize(Roles = "Stjórnendur")]
        [HttpPost]
        public ActionResult Edit(int? id, LinkCategory linkCategory)
        {
            
            
                if (string.IsNullOrEmpty(linkCategory.Name))
                    ModelState.AddModelError("Name", "Þessi reitur er ófylltur");
                if (string.IsNullOrEmpty(linkCategory.Description))
                    ModelState.AddModelError("Description", "Þessi reitur er ófylltur");
            
            try
            {
                if (ModelState.IsValid)
                {
                    if (id.HasValue)
                    {
                        m_Db.updateLinkCategoryById(id.Value, linkCategory);
                        return RedirectToAction("List");
                    }
                    return View(linkCategory);
                }
                return View(linkCategory);
            }
            catch
            {
                return View(linkCategory);
            }
        }


    }
}